#include "delete.hpp"

#include "qbe/src/ecs/app.hpp"

#include "qbe/src/ecs/query.hpp"

#include "qbe/src/axis/axis.hpp"
#include "qbe/src/input/input.hpp"
#include "qbe/src/select/select.hpp"
#include "qbe/src/state/state.hpp"
#include "qbe/src/undoRedo/undoRedo.hpp"
#include "qbe/src/utils/utils.hpp"

#include "qbe/src/layout/layout.hpp"

#include "qbe/src/tools/auto_route_interface.h"
#include "qlib/all.h"

namespace qbe::dele {

void deleteSelectObj(ecs::Query query, ecs::Bus& bus) {
  auto& state = query.get_resource<state::State>();

  // 获取cell和reg
  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;
  auto& layout_cmd = query.get_resource<layout::LayoutCmd>();

  // 获取输入 坐标系 撤销恢复
  auto& handModule = query.get_resource<vsg::ref_ptr<input::HandleBundle>>();
  auto& axisModule = query.get_resource<axis::Axis>();
  auto& undoRedoModule = query.get_resource<undoRedoT::UndoRedoModule>();
  auto& auto_route_interface_object = query.get_resource<qbe::tools::AutoRouteInterfaceObjcet>();

  undoRedoModule.start(cell);

  auto view = reg.view<select::SelectTag>();
  view.each([&reg, &undoRedoModule, &layout_cmd, &auto_route_interface_object](entt::entity e) {
    auto ptr = reg.try_get<qlib::Reference::s_ptr>(e);
    if (ptr) {
      auto del_entity = auto_route_interface_object.deleteReferenceDealWithAutoRoute(*ptr);
      for (auto i : del_entity) {
        undoRedoModule.dele(i);
        layout_cmd.remove(i);
        reg.remove<select::SelectTag>(i);
      }
    }

    undoRedoModule.dele(e);
    layout_cmd.remove(e);

    reg.remove<select::SelectTag>(e);
  });

  undoRedoModule.end();

  auto& sele_event_writer = query.get_event_writer<select::Event>();
  sele_event_writer.send({select::Code::SelectUpdate});
  sele_event_writer.send({select::Code::HoverUpdate});

  auto current_lib = query.get_resource<qlib::Library::s_ptr>();
  bus.send<qbe::UiMessage::UpdateFileLisetFromLibrary>(qbe::UiMessage::UpdateFileLisetFromLibrary{current_lib});
}

// 删除
void update(ecs::Query query, ecs::Bus& bus) {
  // 判断编辑模式为移动
  auto& state = query.get_resource<state::State>();

  // 获取cell和reg
  auto& cell = query.get_resource<qlib::Cell::s_ptr>();
  auto& reg = cell->reg;
  auto& layout_cmd = query.get_resource<layout::LayoutCmd>();

  // 获取输入 坐标系 撤销恢复
  auto& handModule = query.get_resource<vsg::ref_ptr<input::HandleBundle>>();
  auto& axisModule = query.get_resource<axis::Axis>();
  auto& undoRedoModule = query.get_resource<undoRedoT::UndoRedoModule>();
  auto& auto_route_interface_object = query.get_resource<qbe::tools::AutoRouteInterfaceObjcet>();

  bool pressed_esc = qbe::getKeyPressed("edit", "dele obj", bus);

  if (handModule->keyReleased(vsg::KEY_Delete) && state.getDrawingMode() == state::DrawingModeType::None &&
      state.getEditingMode() == state::EditingModeType::None) {
    deleteSelectObj(query, bus);
  }

  // state.exit();
}

void Plugin::build(ecs::App& app) {
  app.add_stage<UpdateStage>();

  app.add_systems<UpdateStage>(update);
}

}  // namespace qbe::dele